package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.act.admin.AdminAct;
import com.rpframework.website.device.domain.Bank;
import com.rpframework.website.device.service.BankService;

@Controller
@RequestMapping("api/bank")
public class ApiBankAct extends AdminAct{
	@Resource BankService service;
	
	@RequestMapping(value="/list",produces = "application/json; charset=utf-8")
	public @ResponseBody String list(@RequestParam(value="pager",required = false ) Pager<Bank> pager,
			@RequestParam(value = "callback",required = false ) String callback
			){
		if(pager==null){
			pager=new Pager<Bank>();
		}
		pager = service.getPager(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<Bank> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(Bank t : list){
				JsonObject obj = new JsonObject();
				obj.addProperty("id", t.getId());	
					obj.addProperty("name", t.getName());
					array.add(obj);
			}
			json.add("arrays", array);
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}	
}
